﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KuMaoPlayer
{
    public partial class Form_Current : Form
    {
        public Form_Current()
        {
            InitializeComponent();
            //listShowSongLrc.DrawMode = DrawMode.OwnerDrawVariable;
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void Form_Current_Load(object sender, EventArgs e)
        {
           
        }

        private void listShowSongLrc_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        internal void change_album(string current_mp3_url)
        {
            // 目前只支持MP3
            if (!current_mp3_url.EndsWith(".mp3"))
            {
                return;
            }
            Image img = null;
            FileStream fs = new FileStream(current_mp3_url, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[10];
            // fs.Read(buffer, 0, 128);
            string mp3ID = "";

            fs.Seek(0, SeekOrigin.Begin);
            fs.Read(buffer, 0, 10);
            int size = (buffer[6] & 0x7F) * 0x200000 + (buffer[7] & 0x7F) * 0x400 + (buffer[8] & 0x7F) * 0x80 + (buffer[9] & 0x7F);
            //int size = (buffer[6] & 0x7F) * 0X200000 * (buffer[7] & 0x7f) * 0x400 + (buffer[8] & 0x7F) * 0x80 + (buffer[9]);
            mp3ID = Encoding.Default.GetString(buffer, 0, 3);
            if (mp3ID.Equals("ID3", StringComparison.OrdinalIgnoreCase))
            {
                //如果有扩展标签头就跨过 10个字节
                if ((buffer[5] & 0x40) == 0x40)
                {
                    fs.Seek(10, SeekOrigin.Current);
                    size -= 10;
                }
                img = ReadFrame(fs, size);
            }

            if (img != null)
            {
                pb_album.Image = img;
            }
        }

        private Image ReadFrame(FileStream fs, int size)
        {
            string[] ID3V2 = new string[6];
            byte[] buffer = new byte[10];
            Image img = null;
            while (size > 0)
            {
                //读取标签帧头的10个字节
                fs.Read(buffer, 0, 10);
                size -= 10;
                //得到标签帧ID
                string FramID = Encoding.Default.GetString(buffer, 0, 4);
                //计算标签帧大小，第一个字节代表帧的编码方式
                int frmSize = 0;

                frmSize = buffer[4] * 0x1000000 + buffer[5] * 0x10000 + buffer[6] * 0x100 + buffer[7];
                if (frmSize == 0)
                {
                    //就说明真的没有信息了
                    break;
                }
                //bFrame 用来保存帧的信息
                byte[] bFrame = new byte[frmSize];
                fs.Read(bFrame, 0, frmSize);
                size -= frmSize;
                string str = GetFrameInfoByEcoding(bFrame, bFrame[0], frmSize - 1);
                if (FramID.CompareTo("TIT2") == 0)
                {
                    ID3V2[0] = "TIT2" + str;
                }
                else if (FramID.CompareTo("TPE1") == 0)
                {
                    ID3V2[1] = "TPE1" + str;
                }
                else if (FramID.CompareTo("TALB") == 0)
                {
                    ID3V2[2] = "TALB" + str;
                }
                else if (FramID.CompareTo("TIME") == 0)
                {
                    ID3V2[3] = "TYER" + str;
                }
                else if (FramID.CompareTo("COMM") == 0)
                {
                    ID3V2[4] = "COMM" + str;
                }
                else if (FramID.CompareTo("APIC") == 0)
                {
                    int i = 0;
                    while (true)
                    {
                        if (255 == bFrame[i] && 216 == bFrame[i + 1])
                        {
                            //在
                            break;
                        }
                        i++;
                    }

                    byte[] imge = new byte[frmSize - i];
                    fs.Seek(-frmSize + i, SeekOrigin.Current);
                    fs.Read(imge, 0, imge.Length);
                    MemoryStream ms = new MemoryStream(imge);
                    img = Image.FromStream(ms);
                }
            }
            return img;
        }

        private string GetFrameInfoByEcoding(byte[] b, byte conde, int length)
        {
            string str = "";
            switch (conde)
            {
                case 0:
                    str = Encoding.GetEncoding("ISO-8859-1").GetString(b, 1, length);
                    break;
                case 1:
                    str = Encoding.GetEncoding("UTF-16LE").GetString(b, 1, length);
                    break;
                case 2:
                    str = Encoding.GetEncoding("UTF-16BE").GetString(b, 1, length);
                    break;
                case 3:
                    str = Encoding.UTF8.GetString(b, 1, length);
                    break;
            }
            return str;
        }

        /*private void listShowSongLrc_DrawItem(object sender, DrawItemEventArgs e)
        {
            //获取当前绘制的行的索引
            int index = e.Index;
            Graphics g = e.Graphics;
            //得到每一项的绘制区域大小
            Rectangle bound = e.Bounds;
            //得到当前项的文本内容
            string text = listShowSongLrc.Items[index].ToString();

            //判断当前选择的项是正在唱的歌词，也就是中间一行歌词
            if (index == listShowSongLrc.Items.Count / 2)
            {//如果当前行为选中行。
             //绘制选中时要显示的蓝色边框，实际不需要就注释掉了
             // g.DrawRectangle(Pens.Blue, bound.Left, bound.Top, bound.Width - 1, bound.Height - 1);
             //绘制边框后，里面的矩形框大小改变，故重新定义一个，如果没有绘制边框就不需要重新定义
                Rectangle rect = new Rectangle(bound.Left - 1, bound.Top - 1,
                                               bound.Width - 2, bound.Height - 2);
                //绘制选中时要显示的蓝色背景。可以选中其它色，此处省略了背景绘制
                // g.FillRectangle(Brushes.Blue, rect);
                //定义一个字体，是用来绘制显示的当前歌词文本。
                Font font = new System.Drawing.Font("微软雅黑", 18, FontStyle.Bold & FontStyle.Italic);
                //绘制歌词，颜色为红色
                TextRenderer.DrawText(g, text, font, rect, Color.Red,
                                      TextFormatFlags.VerticalCenter);
            }
            else
            {
                //定义一个颜色为白色的画刷
                using (Brush brush = new SolidBrush(Color.White))
                {
                    g.FillRectangle(brush, bound);//绘制背景色。
                }
                //填充字体，字体的颜色为黑色
                TextRenderer.DrawText(g, text, this.Font, bound, Color.Black,
                                      TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
            }
        }*/
    }
}
